$(function (){
//   消火栓
    //设备编辑 打开窗口
    $('body').on('click', ".editFireplug-trigger", function (event) {
      $('.devEditSureBtn').hide();
      $('.devEditBtn').show();
      event.preventDefault();
      $('.editFireplug-pop').addClass('is-visible3');
      //获取当前设备信息
      var devCode = $(this).parent().attr('devCode');

      $('#editFireplugForm input.form-control').attr('disabled', 'disabled');
      $('#editFireplugForm select.form-control').attr('disabled', 'disabled');
      $('#editFireplugForm select.form-control').addClass('disabledStyle');
      $('#editFireplugForm input.form-control').addClass('disabledStyle');
      $.ajax({
          url: '/organization/list',
          async: false,
          success: function (res) {
              console.log(res)
              $('#editFireplugModal .orgidval').html('');
              for (var i = 0; i < res.data.length; i++) {
                  $('#editFireplugModal .orgidval').append('<option value="' + res.data[i].id + '">' + res.data[i].orgName + '</option>')
              }
          }
      });
      $.ajax({
          url: '/deviceStatus/get/code?deviceCode=' + devCode,
          async: false,
          success: function (res) {
              console.log(res)
              var deviceid = res.data[0][0].id;
              var deviceName = res.data[0][0].deviceName;
              var usestatus = res.data[0][0].usestatus;
              var deviceTypeName = res.data[0][0].deviceType.deviceTypeName;
              var deviceTypeId = res.data[0][0].deviceType.deviceTypeId;
              var devCode = res.data[0][0].devCode;
              var address = res.data[0][0].address;
              var fixedPlace = res.data[0][0].fixedPlace;
              var latitude = res.data[0][0].latitude;
              var longitude = res.data[0][0].longitude;
              var installationDate = res.data[0][0].installationDate;
              var deadline = res.data[0][0].deadline;
              var linkMain1 = res.data[0][0].linkMain1;
              var linkMain2 = res.data[0][0].linkMain2;
              var linkMain3 = res.data[0][0].linkMain3;
              var linkPhone1 = res.data[0][0].linkPhone1;
              var linkPhone2 = res.data[0][0].linkPhone2;
              var linkPhone3 = res.data[0][0].linkPhone3;
              var remark = res.data[0][0].remark;
              var fireCount = res.data[0][0].fireCount;
              installationDate = installationDate ? moment(installationDate).format("YYYY-MM-DD") : '';
              deadline = deadline ? moment(deadline).format("YYYY-MM-DD") : '';
              $('#editFireplugForm input[name="id"]').val(deviceid);
              $('#editFireplugForm input[name="deviceName"]').val(deviceName);
              $("#editFireplugForm select[name='usestatus']").val(usestatus);
              $('#editFireplugForm input[name="deviceTypeId"]').val(deviceTypeName);
              $('#editFireplugForm input[name="devCode"]').val(devCode);
              $('#editFireplugForm input[name="address"]').val(address);
              $('#editFireplugForm input[name="fixedPlace"]').val(fixedPlace);
              $('#editFireplugForm input[name="latitude"]').val(latitude);
              $('#editFireplugForm input[name="longitude"]').val(longitude);
              $('#editFireplugForm input[name="installationDate"]').val(installationDate);
              $('#editFireplugForm input[name="deadline"]').val(deadline);
              $('#editFireplugForm input[name="linkMain1"]').val(linkMain1);
              $('#editFireplugForm input[name="linkMain2"]').val(linkMain2);
              $('#editFireplugForm input[name="linkMain3"]').val(linkMain3);
              $('#editFireplugForm input[name="linkPhone1"]').val(linkPhone1);
              $('#editFireplugForm input[name="linkPhone2"]').val(linkPhone2);
              $('#editFireplugForm input[name="linkPhone3"]').val(linkPhone3);
              $('#editFireplugForm input[name="remark"]').val(remark);
              $('#editFireplugForm input[name="fireCount"]').val(fireCount);
              var orgname = res.data[0][1];
              $('#editFireplugForm .orgidval').find("option:contains('" + orgname + "')").attr("selected", true);
              $('#editFireplugForm .deviceTypeIdVal').find("option:contains('" + deviceTypeName + "')").attr("selected", true);
          }
      });
      //	$('#editFireplugForm input.form-control').attr('disabled');
  });
  //设备编辑  关闭窗口
  $('.editFireplug-pop').on('click', function (event) {
      if ($(event.target).is('.cd-popup-close') || $(event.target).is('.editFireplug-pop')) {
          event.preventDefault();
          $(this).removeClass('is-visible3');
          //Modal验证销毁重构
          $("#editFireplugForm").data('bootstrapValidator').destroy();
          $('#editFireplugForm').data('bootstrapValidator', null);
          editFireplugFormValidator();
          $('#editFireplugForm input').val('');
      }
  });
  editFireplugFormValidator();

  function editFireplugFormValidator() {
      $("#editFireplugForm").bootstrapValidator({
          message: 'This value is not valid',
          feedbackIcons: {
              valid: 'glyphicon glyphicon-ok',
              invalid: 'glyphicon glyphicon-remove',
              validating: 'glyphicon glyphicon-refresh'
          },
          fields: {
              devCode: {
                  message: '设备编号验证失败',
                  validators: {
                      notEmpty: {
                          message: '设备编号不能为空'
                      }
                  }
              },
              deviceTypeId: {
                  validators: {
                      notEmpty: {
                          message: '设备类型不能为空'
                      }
                  }
              },
              deviceName: {
                  validators: {
                      notEmpty: {
                          message: '设备名称不能为空'
                      }
                  }
              },
              address: {
                  validators: {
                      notEmpty: {
                          message: '安装地址不能为空'
                      }
                  }
              },
              fixedPlace: {
                  validators: {
                      notEmpty: {
                          message: '安装场所不能为空'
                      }
                  }
              },
              //            installationDate: {
              //                validators: {
              //                    notEmpty: {
              //                        message: '安装日期不能为空'
              //                    }
              //                }
              //            },
              latitude: {
                  validators: {
                      notEmpty: {
                          message: '经度不能为空'
                      }
                  }
              },
              longitude: {
                  validators: {
                      notEmpty: {
                          message: '纬度不能为空'
                      }
                  }
              }
          }
      }).on("success.form.bv", function (e) {
          e.preventDefault();
          var $form = $(e.target);
          var nextUrl;
          nextUrl = "/device/save?" + $form.serialize();
          console.log($form.serialize())
          $.ajax({
              url: nextUrl,
              type: 'POST',
              //            data: $form.serialize(),
              success: function (result) {
                  if (result.status == 200) {
                      $("#editFireplugForm").data('bootstrapValidator').destroy();
                      $('#editFireplugForm').data('bootstrapValidator', null);
                      editFireplugFormValidator();
                      $('#editFireplugForm input').val('');
                      $('.editFireplug-pop').removeClass('is-visible3');
                      var devCode = $("#configurationModal .devCodeVal").val() ? '&devCode=' + $("#configurationModal .devCodeVal").val() : '';
                      var status = '';
                      status = $("#configurationModal .devTypeVal").find("option:selected").attr('alarmtype') ? $("#configurationModal .devTypeVal").find("option:selected").attr('alarmtype') : '';

                      if (status == "all") {
                          status = '';
                      } else {
                          status = '&status=' + status;
                      }
                      var deviceTypeId = $('#configurationModal .deviceTypeSearchBtn').val();
                      console.log(configurationPage, devCode, status,deviceTypeId)
                      // 设备管理 删除后重新渲染
                      configurationPageList(configurationPage, devCode, status,deviceTypeId);
                      tipModalFunc('修改成功!');
                  } else {
                      console.log(result.message);
                  }
              }

          });
      });
  }
  //点击修改按钮允许修改
  $("#deviceAllowEdit").on('click', function () {
      $('.devEditSureBtn').show();
      $('.devEditBtn').hide();
      $('#editFireplugForm input.form-control').removeAttr('disabled');
      $('#editFireplugForm select.form-control').removeAttr('disabled');
      $('#editFireplugForm select.form-control').removeClass('disabledStyle');
      $('#editFireplugForm input.form-control').removeClass('disabledStyle');
  });
  //设备添加 打开窗口
  $('body').on('click', '.addFireplug-trigger', function (event) {
      event.preventDefault();
      $('#addFireplugForm select option').eq(0).prop('selected');
      $('.addFireplug-pop').addClass('is-visible3');
      //    var installationDate = moment().format("YYYY-MM-DD");
      //    $('#addFireplugForm .installationDate').val(installationDate);
      $.ajax({
          url: '/organization/list',
          // async:false,
          success: function (res) {
              console.log(res)
              $('#addAccountModal .choiceOrgList').html('');
              $('#addFireplugModal .addAccountOrgList').html('');
              for (var i = 0; i < res.data.length; i++) {
                  $('#addFireplugModal .addAccountOrgList').append('<option value="' + res.data[i].id + '">' + res.data[i].orgName + '</option>')
              }
          }
      });
      $('#addFireplugModal .addAccountOrgList option').eq(0).attr("selected", true);
      $('#addFireplugModal .deviceTypeIdVal option').eq(0).attr("selected", true);
      $('#addFireplugModal .usestatusVal option').eq(0).attr("selected", true);

  });
  //设备添加  关闭窗口
  $('.addFireplug-pop').on('click', function (event) {
      if ($(event.target).is('.cd-popup-close') || $(event.target).is('.addFireplug-pop')) {
          event.preventDefault();
          $(this).removeClass('is-visible3');
          //Modal验证销毁重构
          $("#addFireplugForm").data('bootstrapValidator').destroy();
          $('#addFireplugForm').data('bootstrapValidator', null);
          deviceFormValidator();
          $('#addFireplugForm input').val('');
      }
  });

  deviceFormValidator();

  function deviceFormValidator() {
      $("#addFireplugForm").bootstrapValidator({
          message: 'This value is not valid',
          feedbackIcons: {
              valid: 'glyphicon glyphicon-ok',
              invalid: 'glyphicon glyphicon-remove',
              validating: 'glyphicon glyphicon-refresh'
          },
          fields: {
              devCode: {
                  message: '设备编号验证失败',
                  validators: {
                      notEmpty: {
                          message: '设备编号不能为空'
                      },
                      //                    regexp: {
                      //	                    regexp: /\s+$/
                      ////	                    message: '设备编号只能包含大写、小写、数字和下划线'
                      //	                }	                    

                  }
              },
              deviceTypeId: {
                  validators: {
                      notEmpty: {
                          message: '设备类型不能为空'
                      }
                  }
              },
              deviceName: {
                  validators: {
                      notEmpty: {
                          message: '设备名称不能为空'
                      }
                  }
              },
              address: {
                  validators: {
                      notEmpty: {
                          message: '安装地址不能为空'
                      }
                  }
              },
              fixedPlace: {
                  validators: {
                      notEmpty: {
                          message: '安装场所不能为空'
                      }
                  }
              },
              //            installationDate: {
              //                validators: {
              //                    notEmpty: {
              //                        message: '安装日期不能为空'
              //                    }
              //                }
              //            },
              latitude: {
                  validators: {
                      notEmpty: {
                          message: '经度不能为空'
                      }
                  }
              },
              longitude: {
                  validators: {
                      notEmpty: {
                          message: '纬度不能为空'
                      }
                  }
              }
          }
      }).on("success.form.bv", function (e) {
          e.preventDefault();
          var $form = $(e.target);
          var nextUrl;
          nextUrl = "/device/save",
              console.log($form.serialize())
          $.ajax({
              url: nextUrl,
              type: 'POST',
              data: $form.serialize(),
              success: function (result) {
                  console.log(result)
                  if (result.status == 200) {
                      $('.addFireplug-pop').removeClass('is-visible3');
                      //Modal验证销毁重构
                      $("#addFireplugForm").data('bootstrapValidator').destroy();
                      $('#addFireplugForm').data('bootstrapValidator', null);
                      $('#addFireplugForm input').val('');
                      deviceFormValidator();
                      devSumFunc();
                      devListSum();
                      tipModalFunc('添加成功!');

                      var devCode = $("#configurationModal .devCodeVal").val() ? '&devCode=' + $("#configurationModal .devCodeVal").val() : '';
                      var status = '';
                      status = $("#configurationModal .devTypeVal").find("option:selected").attr('alarmtype') ? $("#configurationModal .devTypeVal").find("option:selected").attr('alarmtype') : '';

                      if (status == "all") {
                          status = '';
                      } else {
                          status = '&status=' + status;
                      }
                      var deviceTypeId = $('#configurationModal .deviceTypeSearchBtn').val();
                      // 设备管理 首次渲染
                      //        	        configurationPageList(configurationPage,devCode,status);
                      console.log(configurationPage, devCode, status,deviceTypeId)
                      // 设备管理 获取总页数 点击分页渲染列表
                      configurationSumPage(configurationPage, devCode, status,deviceTypeId);

                  } else {
                      console.log(result.message);
                  }
              }

          });
      });
  }
});